/**
 * <strong>Title : FailureCause.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.em.controller<br>
 * </strong> <strong>Description : </strong>@类注释说明写在此处@<br>
 * <strong>Create on : 2015年11月4日 下午4:55:40<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.em.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.em.model.FailureCause;
import cn.b.sky.mes.em.model.condition.FailureCauseCondition;
import cn.b.sky.mes.em.service.FailureCauseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

/**
 * <strong>Title : FailureCauseController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.em.controller<br></strong>
 * <strong>Description : </strong>故障原因代码控制器<br> 
 * <strong>Create on : 2015年11月4日 下午4:55:40<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/failureCause/*")
public class FailureCauseController extends SecurityController {

  @Resource
  FailureCauseService failureCauseService;

  @RequestMapping("failureCause_list")
  public String selectFailureCauseList(String navTabId, Model model,
      @ModelAttribute("condition") FailureCauseCondition condition) {
    condition.setSiteCode(getSiteCode());
    List<FailureCause> list = failureCauseService.selectFailureCauseListByCondition(condition);
    int count = failureCauseService.selectFailureCauseCount(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    return "em/failureCause/failureCause_list";
  }

  @RequestMapping("addFailureCause")
  public String addFailureCause(String navTabId, Model model, String id) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "add");
    return "em/failureCause/failureCause_add";
  }

  @RequestMapping("modifyFailureCause")
  public String modifyFailureCause(String navTabId, Model model, String id) {
    FailureCause cause = failureCauseService.getFailureCause(id, getSiteCode());
    model.addAttribute("failureCause", cause);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "modify");
    return "em/failureCause/failureCause_add";
  }

  @RequestMapping("validateFailureCauseCode")
  @ResponseBody
  public boolean validateFailureCauseCode(String failureCauseCode) {
    if (StringUtils.isEmpty(failureCauseCode)) {
      return true;
    }
    FailureCause failureCause = failureCauseService.getFailureCause(failureCauseCode, getSiteCode());
    return failureCause == null;
  }

  @RequestMapping("viewFailureCause")
  public String viewFailureCause(String navTabId, Model model, String id) {
    FailureCause cause = failureCauseService.getFailureCause(id, getSiteCode());
    model.addAttribute("failureCause", cause);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "read");
    return "em/failureCause/failureCause_add";
  }

  @RequestMapping("saveFailureCause")
  @ResponseBody
  public DWZResult saveFailureCause(String navTabId, Model model, String id, FailureCause failureCause) {
    if (failureCause == null) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    if (failureCause.getLockFlag() == null) {
      failureCause.setLockFlag(0);
    }
    setCreateInfo(failureCause);
    failureCause.setSiteCode(getSiteCode());
    if (StringUtils.isBlank(id)) {
      failureCauseService.saveFailureCause(failureCause);
    } else {
      failureCauseService.updateFailureCause(failureCause);

    }
    return dialogAjaxDone(
        getImeViewMessageI18n(null, "successfulOperation.M"),
        navTabId, null, null, null);

  }

  @RequestMapping("deleteFailureCause")
  @ResponseBody
  public DWZResult deleteFailureCause(HttpServletRequest request, String navTabId) {
    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    String[] failureCauseCodes = ids.split(",");
    int len = failureCauseCodes.length;
    if (len == 0) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    BatchJobReport[] reports = new BatchJobReport[len];
    boolean success = true;
    for (int i = 0; i < len; i++) {
      reports[i] = failureCauseService.deleteFailureCause(failureCauseCodes[i], getSiteCode());
      if (!reports[i].isSuccess()) {
        success = false;
      }
    }
    if (success) {
      //直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }
  }

  @RequestMapping("failureCauseLookup")
  public String failureCauseLookup(String navTabId, Model model,
      @ModelAttribute("condition") FailureCauseCondition condition) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    List<FailureCause> list = failureCauseService.selectFailureCauseListByCondition(condition);
    int count = failureCauseService.selectFailureCauseCount(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    return "em/failureCause/failureCause_lookup";
  }
}
